package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.ccl.iot.App;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.IOTObject;
import com.ccl.iot.R;
import com.ccl.iot.bean.ArmPartitionInfoBean;
import com.ccl.iot.bean.ArmZoneInfoBean;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.DVRUtil;
import com.ccl.iot.dvr.utils.HDImageManager;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.dvr.utils.LoggerUtil;
import com.ccl.iot.dvr.utils.TransformUtil;
import com.ccl.iot.object.IOTDeviceInfoEx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.ccl.iot.device.IOTSecurityArmConst.ARMPARTITIONINFO_SIZE;
import static com.ccl.iot.device.IOTSecurityArmConst.ARMPARTITIONINFO_START;
import static com.ccl.iot.device.IOTSecurityArmConst.ARMZONEINFO_START;
import static com.ccl.iot.device.IOTSecurityArmConst.BASE_SIZE;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_ALARMING;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_ARM;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_DISARM;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_UNKNOWN;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_TYPE_IMMEDIATELY;

/**
 * @description：安防主机
 * @author： HD
 * @time：2017/5/4
 */
public class IOTSecurityArm extends IOTDeviceWithState implements IOTObject.IOTDVRInner {
    private String mUdid = null;
    //已经创建的分区集合
    private SparseArray<ArmPartitionInfoBean> mArmPartitionInfoMap = new SparseArray<ArmPartitionInfoBean>();
    //防区集合
    private SparseArray<ArmZoneInfoBean> mArmZoneInfoMap = new SparseArray<ArmZoneInfoBean>();

    //在使用的防区集合
    private List<ArmZoneInfoBean> mOnUseArmZoneInfoList = new ArrayList<ArmZoneInfoBean>();
    private List<String> mArmZoneNameList = new ArrayList<String>();

    public static final int ZONE_CLOSE = 0;
    public static final int ZONE_ALARM = 1;
    public static final int ZONE_OPEN = 2;

    public static final int[] ZONE_STATES = {ZONE_CLOSE, ZONE_ALARM, ZONE_OPEN};

    /**
     * 布防数据
     */
    private byte[] mArmData = new byte[6];

    /**
     * 撤防数据
     */
    private byte[] mDisArmData = new byte[6];

    private byte[] mResetErrorCode = {0,0,0,0};


    /**
     * 错误代码
     */
    private int mErrorCode = -1;

    private int mErrorCount;

    private int mLastErrorCount;

    /**
     * 分区数量
     */
    private int mPartitionCount;

    /**
     * 防区数量
     */
    private int mZoneCount;

    private OnRegDataChangedListener mListener;

    public IOTSecurityArm(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aStateAddress, int aStateSize) {
        super(aDevID, aDeviceInfo, aStateAddress, aStateSize);
    }

    public static boolean isRelativeDevice(IOTSecurityArm device, int address, int size) {
        if (device != null) {
            int armPartitionAddressStart = device.getArmPartitionAddressStart();
            if (armPartitionAddressStart == address && size == IOTSecurityArmConst.ARMPARTITIONINFO_SIZE) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    protected void setStateAddress(int aStateAddress) {
        super.setStateAddress(aStateAddress);
        mDefIndex = 1;
    }

    @Override
    public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
        saveRegister(aAddress, aSize, aData, aNetWork);
        Log.e(Consts.ARM, "RegData[" + (aNetWork == IOTNetAPIEnum.EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_LOCAL
                ? "LOCAL" : "REMOTE") + "]:" + getID() + ", " + aAddress + ", " + aSize + ", " + Arrays.toString(Arrays.copyOf(aData, aSize)));
        int length = aData.length;

        //更新寄存器信息
        if (aData != null && aData.length > 0) {

            if (aAddress == 0 && length >= IOTSecurityArmConst.ARMPARTITIONINFO_SIZE) { // 返回的数据是从地址0开始的


                byte[] errorCountbytes = Arrays.copyOfRange(aData, 24, 26);

                String errorCount = TransformUtil.byteToHexString(errorCountbytes);
                mLastErrorCount = mErrorCount;
                mErrorCount =  Integer.valueOf(errorCount,16);

                byte[] errorCodetbytes = Arrays.copyOfRange(aData, 26, 28);

                String errorCode = TransformUtil.byteToHexString(errorCodetbytes);

                mErrorCode =  Integer.valueOf(errorCode,16);

                LoggerUtil.i(Consts.ARM, "提示--mErrorCount：" + errorCount
                        + "--mErrorCode:" + errorCode
                        + "--Integer.valueOf(errorCode):" + mErrorCode
                + "--mErrorCode:" + Arrays.toString(Arrays.copyOf(errorCodetbytes, 2)));


//                byte[] partitionCountData = Arrays.copyOfRange(aData, 28, 29);
//                mPartitionCount = (int) TransformUtil.bytes2Long(partitionCountData);
//
//                byte[] zoneCountData = Arrays.copyOfRange(aData, 30, 31);
//                mZoneCount = (int) TransformUtil.bytes2Long(zoneCountData);

                //分区的数据
                byte[] allArmPartitionInfoData = Arrays.copyOfRange(aData, ARMPARTITIONINFO_START, 80);
                Log.i(Consts.ARM, "--allArmPartitionInfoData:" + TransformUtil.ArrayTo16String(allArmPartitionInfoData));
                int armPartitionIndex = 0;
                for (int i = 0; i < 48; i += ARMPARTITIONINFO_SIZE) {

                    //单个分区的数据
                    byte[] armPartitionInfo = Arrays.copyOfRange(allArmPartitionInfoData, i, i + ARMPARTITIONINFO_SIZE);

//                        Log.i(Consts.ARM,"位置：" + armPartitionIndex + "--起始位:" + (ARMPARTITIONINFO_START + i) +
//                                "--armPartitionInfo:" +  TransformUtil.ArrayTo16String(armPartitionInfo));

                    if (armPartitionInfo != null) {
                        ArmPartitionInfoBean armPartitionInfoBean = mArmPartitionInfoMap.get(armPartitionIndex);
                        if (armPartitionInfoBean == null) {
                            mArmPartitionInfoMap.put(armPartitionIndex, new ArmPartitionInfoBean(ARMPARTITIONINFO_START + i, armPartitionInfo, armPartitionIndex));
                        } else {
                            armPartitionInfoBean.init(ARMPARTITIONINFO_START + i, armPartitionInfo, armPartitionIndex);
                        }
                    }

                    armPartitionIndex++;
                }

                //检查数据的log
//                for (int i = 0; i < mArmPartitionInfoMap.size(); i++) {
//                    int key = mArmPartitionInfoMap.keyAt(i);
//                    Log.i(Consts.ARM,"位置：" + i+"--mArmPartitionInfoMap:" +  TransformUtil.ArrayTo16String(mArmPartitionInfoMap.get(key).getData()));
//                }

                //防区的数据
                byte[] armZoneInfoData = Arrays.copyOfRange(aData, ARMZONEINFO_START, 112);
                Log.i(Consts.ARM, "--armZoneInfoData:" + TransformUtil.ArrayTo16String(armZoneInfoData));
                for (int i = 0; i < 32; i++) {
                    byte armZoneInfo = armZoneInfoData[i];
//                    Log.i(Consts.ARM,"位置：" + i+"--armZoneInfoData:" +  String.format("%02X", armZoneInfo));
                    //单个防区的数据
                    ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(i);
                    if (armZoneInfoBean == null) {
                        mArmZoneInfoMap.put(i, new ArmZoneInfoBean(ARMZONEINFO_START + i, armZoneInfo, i));
//                            Log.i(Consts.ARM,"新建：" + i+"--armZoneInfoData:" +  String.format("%02X", armZoneInfo));
                    } else {
                        armZoneInfoBean.init(ARMZONEINFO_START + i, armZoneInfo, i);
                    }

                }
            } else if (aAddress == 24 && length >= IOTSecurityArmConst.ARMPARTITIONINFO_SIZE) { // 返回的数据是从地址0开始的

//                byte[] partitionCountData = Arrays.copyOfRange(aData, 4, 6);
//                mPartitionCount = (int) TransformUtil.bytes2Long(partitionCountData);
//
//                byte[] zoneCountData = Arrays.copyOfRange(aData, 6, 9);
//                mZoneCount = (int) TransformUtil.bytes2Long(zoneCountData);



//                byte[] errorCountbytes = Arrays.copyOfRange(aData, 0, 2);
//
//                mErrorCount = TransformUtil.bytesToInt(errorCountbytes,0);
//
//                byte[] errorCodetbytes = Arrays.copyOfRange(aData, 2, 4);
//
//                mErrorCode = TransformUtil.bytesToInt(errorCodetbytes,0);
//
//                LoggerUtil.i(Consts.ARM, "--2--提示mErrorCount：" + mErrorCount + "--mErrorCode:" + mErrorCode);

                byte[] errorCountbytes = Arrays.copyOfRange(aData, 0, 2);

                String errorCount = TransformUtil.byteToHexString(errorCountbytes);
                mLastErrorCount = mErrorCount;
                mErrorCount =  Integer.valueOf(errorCount,16);

                byte[] errorCodetbytes = Arrays.copyOfRange(aData, 2, 4);

                String errorCode = TransformUtil.byteToHexString(errorCodetbytes);

                mErrorCode =  Integer.valueOf(errorCode,16);

                LoggerUtil.i(Consts.ARM, "提示2--mErrorCount：" + errorCount
                        + "--mErrorCode:" + errorCode
                        + "--Integer.valueOf(errorCode):" + mErrorCode
                        + "--mErrorCode:" + Arrays.toString(Arrays.copyOf(errorCodetbytes, 2)));


                //分区的数据
                byte[] allArmPartitionInfoData = Arrays.copyOfRange(aData, 8, 56);
                Log.i(Consts.ARM, "--allArmPartitionInfoData:" + TransformUtil.ArrayTo16String(allArmPartitionInfoData));
                int armPartitionIndex = 0;
                for (int i = 0; i < 48; i += ARMPARTITIONINFO_SIZE) {

                    //单个分区的数据
                    byte[] armPartitionInfo = Arrays.copyOfRange(allArmPartitionInfoData, i, i + ARMPARTITIONINFO_SIZE);

//                        Log.i(Consts.ARM,"位置：" + armPartitionIndex + "--起始位:" + (ARMPARTITIONINFO_START + i) +
//                                "--armPartitionInfo:" +  TransformUtil.ArrayTo16String(armPartitionInfo));

                    if (armPartitionInfo != null) {
                        ArmPartitionInfoBean armPartitionInfoBean = mArmPartitionInfoMap.get(armPartitionIndex);
                        if (armPartitionInfoBean == null) {
                            mArmPartitionInfoMap.put(armPartitionIndex, new ArmPartitionInfoBean(ARMPARTITIONINFO_START + i, armPartitionInfo, armPartitionIndex));
                        } else {
                            armPartitionInfoBean.init(ARMPARTITIONINFO_START + i, armPartitionInfo, armPartitionIndex);
                        }
                    }

                    armPartitionIndex++;
                }

                //检查数据的log
//                for (int i = 0; i < mArmPartitionInfoMap.size(); i++) {
//                    int key = mArmPartitionInfoMap.keyAt(i);
//                    Log.i(Consts.ARM,"位置：" + i+"--mArmPartitionInfoMap:" +  TransformUtil.ArrayTo16String(mArmPartitionInfoMap.get(key).getData()));
//                }

                //防区的数据
                byte[] armZoneInfoData = Arrays.copyOfRange(aData, 56, 88);
                Log.i(Consts.ARM, "--armZoneInfoData:" + TransformUtil.ArrayTo16String(armZoneInfoData));
                for (int i = 0; i < 32; i++) {
                    byte armZoneInfo = armZoneInfoData[i];
//                    Log.i(Consts.ARM,"位置：" + i+"--armZoneInfoData:" +  String.format("%02X", armZoneInfo));
                    //单个防区的数据
                    ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(i);
                    if (armZoneInfoBean == null) {
                        mArmZoneInfoMap.put(i, new ArmZoneInfoBean(ARMZONEINFO_START + i, armZoneInfo, i));
//                      Log.i(Consts.ARM,"新建：" + i+"--armZoneInfoData:" +  String.format("%02X", armZoneInfo));
                    } else {
                        armZoneInfoBean.init(ARMZONEINFO_START + i, armZoneInfo, i);
                    }

                }

                //检查数据的log
//                for (int i = 0; i < mArmZoneInfoMap.size(); i++) {
//                    int key = mArmZoneInfoMap.keyAt(i);
//                    Log.i(Consts.ARM,"位置：" + i+"--key:" +  String.format("%02X", mArmZoneInfoMap.get(key).getData()));
//                }

            }
        }

        if (mListener != null) {
            mListener.onRegDataChange();
        }

        if (mOnErrorCodeListener != null) {
            mOnErrorCodeListener.onErrorCodeChange();
        }

        afterStateChange();

    }


    @Override
    public String[] getStateIDs() {
        return null;
    }

    /**
     * 获取分区信息集合
     *
     * @return
     */
    public SparseArray<ArmPartitionInfoBean> getArmPartitionInfoMap() {
        return mArmPartitionInfoMap;
    }

    /**
     * 获取防区信息集合
     *
     * @return
     */
    public SparseArray<ArmZoneInfoBean> getArmZoneInfoMap() {
        return mArmZoneInfoMap;
    }


    /**
     * 获取在使用的防区信息集合
     *
     * @return
     */
    public List<ArmZoneInfoBean> getOnUseArmZoneInfoList() {
        return mOnUseArmZoneInfoList;
    }

    /**
     * @return true 有相同默认名字
     */
    public boolean checkArmZoneName(){
        if (mArmZoneInfoMap != null && mArmZoneInfoMap.size() > 0){
            mArmZoneNameList.clear();
            int size = mArmZoneInfoMap.size();
            for (int i = 0; i < size; i++) {
                ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(i);
                String name = armZoneInfoBean.getName();
                if (name.contains(App.getContext().getResources().getString(R.string.armZone))){
                    if (!mArmZoneNameList.contains(name)){
                        mArmZoneNameList.add(name);
                    }else {
                        return true;
                    }
                }
            }
            return false;
        }
        return true;
    }

    /**
     * 获取在使用的防区信息集合
     *
     * @return
     */
    public List<ArmZoneInfoBean> initOnUseArmZoneInfoListFromNet() {

        mOnUseArmZoneInfoList.clear();

        for (int i = 0; i < 32; i++) {
            int iconData = getIconData(i);
//            Log.e("LOCK", "iconData:" + iconData + "--名字：" + getName(i));
//            LoggerUtil.i("LOCK","数据：" +iconData);
            ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(i);

            if (armZoneInfoBean != null) {

                String armZonenName = getName(i);
                if (armZonenName.equals(getName())) {
                    armZonenName = App.getContext().getResources().getString(R.string.armZone) + (i + 1);
                }

                armZoneInfoBean.setName(armZonenName);

                if (iconData < 128 && iconData >= 0) {
                    //显示的设备
                    armZoneInfoBean.setShow(true);
                    if (!mOnUseArmZoneInfoList.contains(armZoneInfoBean)) {
//                    LoggerUtil.e("LOCK","添加--");
                        mOnUseArmZoneInfoList.add(armZoneInfoBean);
                    }
                } else if (iconData >= 0) {
                    //隐藏的设备
                    armZoneInfoBean.setShow(false);
                    if (mOnUseArmZoneInfoList.contains(armZoneInfoBean)) {
//                    Log.e("LOCK", "减少--");
                        mOnUseArmZoneInfoList.remove(armZoneInfoBean);
                    }
                }
            }


        }
        return mOnUseArmZoneInfoList;
    }

    /**
     * 获取在使用的防区信息集合
     *
     * @return
     */
    public List<ArmZoneInfoBean> initOnUseArmZoneInfoListFromLocal() {

        for (int i = 0; i < 32; i++) {
            ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(i);
            if (armZoneInfoBean != null) {

                boolean isShow = armZoneInfoBean.isShow();
//            Log.i("LOCK", "显示：" + isShow + "--名字：" + armZoneInfoBean.getName());

                if (isShow) {
                    //显示的设备
                    if (!mOnUseArmZoneInfoList.contains(armZoneInfoBean)) {
//                    Log.i("LOCK", "添加：" + i+ "--名字：" + armZoneInfoBean.getName());
                        mOnUseArmZoneInfoList.add(armZoneInfoBean);
                    }
                } else {
                    //隐藏的设备
                    if (mOnUseArmZoneInfoList.contains(armZoneInfoBean)) {
//                    Log.i("LOCK", "减少：" + i+ "--名字：" + armZoneInfoBean.getName());
                        mOnUseArmZoneInfoList.remove(armZoneInfoBean);
                    }
                }
            }

        }
        return mOnUseArmZoneInfoList;
    }

    public int getOnUseArmZoneInfoListIndex(int armZoneIndex) {

        if (mOnUseArmZoneInfoList != null && mOnUseArmZoneInfoList.size() > 0) {
            int size = mOnUseArmZoneInfoList.size();
            for (int i = 0; i < size; i++) {
                int index = mOnUseArmZoneInfoList.get(i).getIndex();
                if (index == armZoneIndex) {
                    return i;
                }
            }
        }
        return -1;
    }


    /**
     * 发送布撤防指令
     *
     * @param pwd
     */
    public void sendCMD(int length, int pwd, boolean isArm) {
        if (length == 4) {
            if (pwd >= 0 && pwd <= 9999) {

                savePWD2Data(4, pwd, isArm);

                if (isArm) {
                    iRegisterInterface.writeRegister(this, ARMPARTITIONINFO_START, mArmData.length, mArmData, 2);
                } else {
                    iRegisterInterface.writeRegister(this, ARMPARTITIONINFO_START, mDisArmData.length, mDisArmData, 2);
                }
                LogUtil.e(Consts.ARM,  "发送指令" + TransformUtil.ArrayTo16String(mArmData));
//                LoggerUtil.i(Consts.ARM, "发送指令" + TransformUtil.ArrayTo16String(mArmData));
            }

        } else if (length == 6) {

        }

    }

    /**
     * 保存分区的信息
     *
     * @param length
     * @param pwd
     * @param isArm
     */
    public void saveArmZoneData(int length, int pwd, boolean isArm) {
        if (length == 4) {
            if (pwd >= 0 && pwd <= 9999) {
                savePWD2Data(4, pwd, isArm);
            }

        } else if (length == 6) {

        }
    }

    /**
     * 保存密码数据
     *
     * @param length
     * @param pwd
     * @param isArm
     */
    public void savePWD2Data(int length, int pwd, boolean isArm) {
        String pwsString = String.valueOf(pwd);
        //转成16进制int
        Integer pwdTemp = Integer.valueOf(pwsString, 16);

        if (isArm) {
            mArmData[0] = EARM_STATE_ARM;
            mArmData[1] = EARM_TYPE_IMMEDIATELY;
            mArmData[2] = (byte) length;
            mArmData[3] = (byte) (pwdTemp & 0xFF);
            mArmData[4] = (byte) ((pwdTemp >> 8) & 0xFF);
            mArmData[5] = (byte) ((pwdTemp >> 16) & 0xFF);
//            LoggerUtil.i(Consts.ARM, "保存mArmZoneData:" + TransformUtil.ArrayTo16String(mArmData));
        } else {
            mDisArmData[0] = EARM_STATE_DISARM;
            mDisArmData[1] = EARM_TYPE_IMMEDIATELY;
            mDisArmData[2] = (byte) length;
            mDisArmData[3] = (byte) (pwdTemp & 0xFF);
            mDisArmData[4] = (byte) ((pwdTemp >> 8) & 0xFF);
            mDisArmData[5] = (byte) ((pwdTemp >> 16) & 0xFF);
//            LoggerUtil.i(Consts.ARM, "保存mArmZoneData:" + TransformUtil.ArrayTo16String(mDisArmData));
        }

    }

    /**
     * 获取分区的信息
     *
     * @return
     */
    public byte[] getArmPartitionInfo() {

        ArmPartitionInfoBean armPartitionInfoBean = mArmPartitionInfoMap.get(0);
        if (armPartitionInfoBean != null) {
            byte mArmPartitionState = armPartitionInfoBean.getState();

            if (mArmPartitionState == EARM_STATE_DISARM
                    || mArmPartitionState == EARM_STATE_UNKNOWN) {
                //撤防
                return mDisArmData;

            } else if (mArmPartitionState == EARM_STATE_ARM
                    || mArmPartitionState == EARM_STATE_ALARMING) {
                //设防
                return mArmData;
            }
        }

        return mArmData;
    }


    /**
     * 设置名字
     */
    public boolean setName(long aIndex, String aName) {
        int tmp = mDefIndex;
        mDefIndex = (int) (aIndex + 2);
        mName = null;
        mNameLabel = null;
        boolean result = super.setName(aName);
        mDefIndex = tmp;
        mName = null;
        mNameLabel = null;
        return result;
    }

    /**
     * 设置图标
     */
    public boolean setIcon(long aIndex, String icon) {
        int tmp = mDefIndex;
        mDefIndex = (int) (aIndex + 2);
        mIcon = null;
        mIconLabel = null;
        boolean result = super.setIcon(icon);
        mDefIndex = tmp;
        mIcon = null;
        mIconLabel = null;
        return result;
    }

    /**
     * 隐藏防区
     *
     * @param hide
     * @param index
     */
    public void setHide(boolean hide, int index) {

        int iconId = getIcon(index);

        if (hide) {
            iconId = iconId | 0x40000000;
        } else {
            iconId = iconId & 0xff;
        }

//        Log.i("LOCK", "iconId:" + iconId + "--下标：" + index);
        setIcon(index, iconId + "");
    }

    /**
     * 根据下表获取名字
     *
     * @param index
     * @return
     */
    public String getName(int index) {
        int tmp = mDefIndex;
        mDefIndex = index + 2;
        mName = null;
        mNameLabel = null;
        String name = super.getName();
        mDefIndex = tmp;
        mName = null;
        mNameLabel = null;
        return name;
    }

    /**
     * 根据下表获取图片
     *
     * @param index
     * @return
     */
    public int getIcon(int index) {
        int tmp = mDefIndex;
        mDefIndex = index + 2;
        mIcon = null;
        mIconLabel = null;
        int icon = super.getSplitIconId();
        mDefIndex = tmp;
        mIcon = null;
        mIconLabel = null;
        return icon;
    }

    @Override
    public void onNameChange(String newName) {
        super.onNameChange(newName);
    }

    @Override
    public void onIconChange(String newIcon) {
        super.onIconChange(newIcon);
    }

    /**
     * 获取防区状态
     *
     * @param index
     * @return
     */
    public int getZoneImgState(int index) {
        ArmZoneInfoBean armZoneInfoBean = mArmZoneInfoMap.get(index);
        return getZoneImgState(armZoneInfoBean);
    }

    /**
     * 获取防区状态
     *
     * @param armZoneInfoBean
     * @return
     */
    public int getZoneImgState(ArmZoneInfoBean armZoneInfoBean) {

        int state = armZoneInfoBean.getState();
        int lineState = armZoneInfoBean.getLineState();
        int imgID = 0;

        if (lineState == 0) {
            //闭路
            switch (state) {
                case 0:
                    imgID = ZONE_CLOSE;
                    break;
                case 1:
                    imgID = ZONE_ALARM;
                    break;
                default:
                    break;
            }
        } else if (lineState == 1) {
            //开路
            switch (state) {
                case 0:
                    imgID = ZONE_OPEN;
                    break;
                case 1:
                    imgID = ZONE_ALARM;
                    break;
                default:
                    imgID = ZONE_OPEN;
                    break;
            }
        }

        return imgID;
    }

    /**
     * 获取图片的id
     *
     * @param zoneIndex 防区下标
     * @param state     防区的状态
     * @return
     */
    public String getImageID(int zoneIndex, int state) {
        return "DEV." + getUDID() + "." + zoneIndex + "." + state;
    }


    public boolean setImage(int zoneIndex, Bitmap bitmap) {
//        //状态
//        int zoneImgState = getZoneImgState(zoneIndex);
//        //状态图片id
//        String imageID = getImageID(zoneIndex,zoneImgState);
//
//        if (bitmap == null) {
//            if (ImageManager.RemoveBitmap(imageID)) {
//                return true;
//            }
//        } else {
//            ImageManager.SetBitmap(imageID, bitmap);
//            return true;
//        }
//        return false;

        //状态
        int zoneImgState = getZoneImgState(zoneIndex);
        //状态图片id
        String imageID = getImageID(zoneIndex, zoneImgState);

        if (bitmap != null) {
            return HDImageManager.getInstince().setBitmap(imageID, bitmap);
        }

        return false;
    }

    @Override
    public boolean setImage(Bitmap image) {
        return setImage(DVRUtil.zoneIndex, image);
    }


    public Bitmap getImage(int zoneIndex) {
//        if (getImageCount(zoneIndex) > 0){
//            //状态
//            int zoneImgState = getZoneImgState(zoneIndex);
//            //状态图片id
//            String imageID = getImageID(zoneIndex,zoneImgState);
//
//            Bitmap bitmap = ImageManager.GetBitmap(imageID);
//            if (bitmap == null){
//                int length = ZONE_STATES.length;
//                for (int i = 0; i < length; i++) {
//                    String zoneImageID = getImageID(zoneIndex,ZONE_STATES[i]);
//                    bitmap = ImageManager.GetBitmap(zoneImageID);
//                    if (bitmap != null){
//                        break;
//                    }
//                }
//            }
//
//            return bitmap;
//        }
//        return null;

        if (getImageCount(zoneIndex) > 0) {
            //状态
            int zoneImgState = getZoneImgState(zoneIndex);
            //状态图片id
            String imageID = getImageID(zoneIndex, zoneImgState);

            Bitmap bitmap = HDImageManager.getInstince().getBitmap(imageID);
            if (bitmap == null) {
                int length = ZONE_STATES.length;
                for (int i = 0; i < length; i++) {
                    String zoneImageID = getImageID(zoneIndex, ZONE_STATES[i]);
                    bitmap = HDImageManager.getInstince().getBitmap(zoneImageID);
                    if (bitmap != null) {
                        break;
                    }
                }
            }

            return bitmap;
        }
        return null;
    }

    public String getImagePath(int zoneIndex) {

        if (getImageCount(zoneIndex) > 0) {
            //状态
            int zoneImgState = getZoneImgState(zoneIndex);
            //状态图片id
            String imageID = getImageID(zoneIndex, zoneImgState);

            String bitmapPath = HDImageManager.getInstince().getBitmapPath(imageID);

            if (TextUtils.isEmpty(bitmapPath)) {
                int length = ZONE_STATES.length;
                for (int i = 0; i < length; i++) {
                    String zoneImageID = getImageID(zoneIndex, ZONE_STATES[i]);
                    bitmapPath = HDImageManager.getInstince().getBitmapPath(zoneImageID);
                    if (!TextUtils.isEmpty(bitmapPath)) {
                        break;
                    }
                }
            }

            return bitmapPath;
        }
        return null;
    }

    @Override
    public Bitmap getImage() {
        return getImage(DVRUtil.zoneIndex);
    }

    public int getImageCount(int zoneIndex) {

//        int length = ZONE_STATES.length;
//        int count = 0;
//        for (int i = 0; i < length; i++) {
//            String imageID = getImageID(zoneIndex,ZONE_STATES[i]);
//            if (ImageManager.ContainsBitmap(imageID)){
//                count++;
//            }
//        }
//        return count;

        int length = ZONE_STATES.length;
        int count = 0;
        for (int i = 0; i < length; i++) {
            String imageID = getImageID(zoneIndex, ZONE_STATES[i]);
            if (HDImageManager.getInstince().containsBitmap(imageID)) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getImageCount() {
        return getImageCount(DVRUtil.zoneIndex);
    }

    /**
     * 根据下表获取扩展信息
     *
     * @param index
     * @return
     */
    public int getIconData(int index) {
        int tmp = mDefIndex;
        mDefIndex = index + 1;
        int iconData = super.getNoSplitIconData();
        mDefIndex = tmp;
        return iconData;
    }


    @Override
    public int getRegisterAddress() {
//        LoggerUtil.i("super.getRegisterAddress():" +super.getRegisterAddress() +
//                "--super.getRegisterAddress() + BASEADDRESS:" + super.getRegisterAddress() + IOTSecurityArmConst.BASE_SIZE);
        return super.getRegisterAddress() + BASE_SIZE;
    }

    public int getArmPartitionAddressStart() {
        return IOTSecurityArmConst.ARMPARTITIONINFO_START;
    }

    @Override
    public byte[] getRegister() {
        return mArmData;
    }

    @Override
    public String getUDID() {
        if (mUdid == null) {
            mUdid = super.getUDID().replace('-', '@');
        }
        return mUdid;
    }

    @Override
    public int getCornerId() {
        return R.drawable.icon_alart;
    }


    public interface OnRegDataChangedListener {
         void onRegDataChange();
    }

    private OnSelectedChangeListener mOnSelectedChangeListener;

    public interface OnSelectedChangeListener {
        void onSelectedChange();
    }

    public void setOnSelectedChangeListener(OnSelectedChangeListener listener) {
        mOnSelectedChangeListener = listener;
    }

    public void onSelectedChangeEvent(){
        if (mOnSelectedChangeListener != null){
            mOnSelectedChangeListener.onSelectedChange();
        }
    }


    private OnErrorCodeListener mOnErrorCodeListener;

    public interface OnErrorCodeListener {
         void onErrorCodeChange();
    }

    public void setOnErrorCodeListener(OnErrorCodeListener listener) {
        mOnErrorCodeListener = listener;
    }


    public void setOnRegDataChangedListener(OnRegDataChangedListener listener) {
        mListener = listener;
    }

    private OnDisplayChangeListener mOnDisplayChangeListener;

    public void setOnDisplayChangeListener(OnDisplayChangeListener l) {
        mOnDisplayChangeListener = l;
    }

    public interface OnDisplayChangeListener {
        void onDisplayChange();
    }

    private OnNameChangeListener mOnNameChangeListener;

    public void setOnNameChangeListener(OnNameChangeListener l) {
        mOnNameChangeListener = l;
    }

    public interface OnNameChangeListener {
        void onNameChange();
    }

    /**
     * 显示与隐藏的回调
     */
    public void onDisplayChangeEvent() {
        if (mOnDisplayChangeListener != null) {
            mOnDisplayChangeListener.onDisplayChange();
        }
    }

    /**
     * 名字改变的回调
     */
    public void onNameChangeEvent() {
        if (mOnNameChangeListener != null) {
            mOnNameChangeListener.onNameChange();
        }
    }

    public void onStyleChangeEvent() {
        if (mOnNameChangeListener != null) {
            mOnNameChangeListener.onNameChange();
        }
    }

    public void onRegDataChangeEvent() {
        if (mListener != null) {
            mListener.onRegDataChange();
        }
    }

    @Override
    public int getImageId() {
        ArmPartitionInfoBean armPartitionInfoBean = mArmPartitionInfoMap.get(0);
        int ids = R.drawable.icon_security_disarm;
        int imgIndex = getSplitIconId();

        if (imgIndex >= DeviceIconProvider.ICON_LIST_SECURITY_ON.length) {
            imgIndex = 0;
        }

        if (armPartitionInfoBean != null) {
            byte state = armPartitionInfoBean.getState();

            switch (state) {
                case EARM_STATE_ARM:
                    ids = DeviceIconProvider.ICON_LIST_SECURITY_ON[imgIndex];
                    break;
                case EARM_STATE_DISARM:
                    ids = DeviceIconProvider.ICON_LIST_SECURITY_OFF[imgIndex];
                    break;
                case EARM_STATE_ALARMING:
                    ids = DeviceIconProvider.ICON_LIST_SECURITY_ON[imgIndex];
                    break;
                default:
                    ids = DeviceIconProvider.ICON_LIST_SECURITY_OFF[imgIndex];
                    break;
            }
        }

        return ids;
    }

    @Override
    public boolean isHide() {
        return false;
    }

    public int getErrorCode(){
        return mErrorCode;
    }

    public int getErrorCount(){
        return mErrorCount;
    }

    public int getLastErrorCount(){
        return mLastErrorCount;
    }
}
